#include <ESP8266WiFi.h>
#include <ArduinoJson.h>/* const size_t BUFFER_SIZE = 800;  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer; */
#ifndef STASSID
#define STASSID "BlackWalnut"
#define STAPSK "blackwalnut"
#endif

/* 希望保留的天气数据 */
struct WeatherData
{
  char city[50];    //城市名称
  char udate[50];   //更新时间
  char temp[50];    //温度
  char weather[50]; //天气介绍(多云...)
} weather_now = {"", "", "", ""};

const char *ssid = STASSID;
const char *password = STAPSK;
const char *host = "47.93.197.229";
const uint16_t port = 10002;

WiFiClient client; /* Use WiFiClient class to create TCP connections */
void read_client(int delay_num, char *city, char *udate, char *temp, char *weather)
{
  while (!client.connect(host, port))
  {
    Serial.println("Client connection failed");
    delay(500);
    Serial.println("Try Again");
  }
  delay(delay_num);

  /********************** Decode Start ****************************/
  char body[800] = "";
  strcpy(body, client.readString().c_str());
  const size_t capacity = 2 * JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(14) + 380;
  DynamicJsonBuffer jsonBuffer(capacity);
  JsonObject &root = jsonBuffer.parseObject(body);
  JsonObject &results_0 = root["results"][0];
  JsonObject &results_0_location = results_0["location"];
  JsonObject &results_0_daily_0 = results_0["daily"][0];

  /********************** fetch data ****************************/
  const char *results_0_location_name = results_0_location["name"];           // "杭州"
  const char *results_0_daily_0_date = results_0_daily_0["date"];             // "2020-11-04"
  const char *results_0_daily_0_high = results_0_daily_0["high"];             // "17"
  const char *results_0_daily_0_low = results_0_daily_0["low"];               // "10"
  const char *results_0_daily_0_text_day = results_0_daily_0["text_day"];     // "晴"
  const char *results_0_daily_0_text_night = results_0_daily_0["text_night"]; // "多云"

  /********************** restore data ****************************/
  strcpy(city, results_0_location_name);
  strcpy(udate, results_0_daily_0_date);
  snprintf(temp, 50, "%s ~ %s 摄氏度", results_0_daily_0_high, results_0_daily_0_low);
  snprintf(weather, 50, "%s 转 %s", results_0_daily_0_text_day, results_0_daily_0_text_night);
}
String format_String(const char *format, float x, float y, float z)
{
  char ch[100];
  sprintf(ch, format, x, y, z);
  String out = "";
  for (int i = 0; ch[i] != '\0'; i++)
    out += ch[i];
  return out;
}
void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA); /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void loop()
{
  read_client(50, weather_now.city, weather_now.udate, weather_now.temp, weather_now.weather);
  Serial.println("\n得到的天气信息是：");
  Serial.println(weather_now.city);
  Serial.println(weather_now.udate);
  Serial.println(weather_now.temp);
  Serial.println(weather_now.weather);
}
/**
 * 
{
  "results": [
    {
      "location": {
        "id": "WTMKQ069CCJ7",
        "name": "杭州",
        "country": "CN",
        "path": "杭州,杭州,浙江,中国",
        "timezone": "Asia/Shanghai",
        "timezone_offset": "+08:00"
      },
      "daily": [
        {
          "date": "2020-11-04",
          "text_day": "晴",
          "code_day": "0",
          "text_night": "多云",
          "code_night": "4",
          "high": "17",
          "low": "10",
          "rainfall": "0.0",
          "precip": "",
          "wind_direction": "东",
          "wind_direction_degree": "90",
          "wind_speed": "15",
          "wind_scale": "3",
          "humidity": "59"
        }
      ],
      "last_update": "2020-11-03T11:00:00+08:00"
    }
  ]
}
 */